package team.knowers.tourist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.tourist.common.database.BaseDO;
import team.knowers.tourist.dao.entity.BookingDO;
import team.knowers.tourist.dao.entity.FlightDO;
import team.knowers.tourist.dao.entity.NoticeDO;
import team.knowers.tourist.dao.entity.PaymentDO;
import team.knowers.tourist.dao.mapper.BookingMapper;
import team.knowers.tourist.dao.mapper.FlightMapper;
import team.knowers.tourist.dao.mapper.NoticeMapper;
import team.knowers.tourist.dao.mapper.PaymentMapper;
import team.knowers.tourist.dto.req.BookingCreateReqDTO;
import team.knowers.tourist.service.NoticeService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeDO> implements NoticeService {
    private final FlightMapper flightMapper;
    private final NoticeMapper noticeMapper;
    private final BookingMapper bookingMapper;
    private final PaymentMapper paymentMapper;
    @Override
    public List<NoticeDO> getNoticesPerPeople(int userId) {
        LambdaQueryWrapper<BookingDO> eq = Wrappers.lambdaQuery(BookingDO.class).eq(BaseDO::getDelFlag, 0).eq(BookingDO::getTouristId, userId).orderByDesc(BaseDO::getCreateTime);
        List<BookingDO> bookingDOS = bookingMapper.selectList(eq);
        //获得所有航班号
        List<Integer> flightIds = bookingDOS.stream().map(BookingDO::getFlightId).collect(Collectors.toList());
        if(flightIds.isEmpty()) {
            return null;
        }
        return noticeMapper.selectList(Wrappers.lambdaQuery(NoticeDO.class)
                .eq(BaseDO::getDelFlag, 0)
                .and(wrapper -> wrapper.isNull(NoticeDO::getUserId)
                        .or().eq(NoticeDO::getUserId, userId))
                .in(NoticeDO::getFlightId, flightIds)
                .orderByDesc(NoticeDO::getCreateTime));
    }

    @Override
    public Boolean addNoticeWhenBooking(BookingCreateReqDTO req,int a) {
        FlightDO flightDO = flightMapper.selectById(req.getFlightId());
        String title = "您已成功预定"+flightDO.getFlightNo()+"次航班";
        String content = "您已成功预定"+flightDO.getFlightNo()+"次航班,请牢记您的预订编号以进行后续取票,预定编号："+a;
        NoticeDO build = NoticeDO.builder()
                .title(title)
                .content(content)
                .userId(req.getTouristId())
                .flightId(req.getFlightId())
                .flightNo(flightDO.getFlightNo())
                .build();
        return noticeMapper.insert(build) > 0;
    }

    @Override
    public Boolean addNoticeWhenPaying(int paymentId) {
        PaymentDO paymentDO = paymentMapper.selectById(paymentId);
        String title = "您已成功支付"+paymentDO.getFlightNo()+"次航班订单";
        String content = "您已成功支付"+paymentDO.getFlightNo()+"次航班订单,请牢记您的支付编号以进行后续取票，支付编号："+paymentDO.getPaymentHash();
        NoticeDO build = NoticeDO.builder()
                .title(title)
                .content(content)
                .userId(paymentDO.getTouristId())
                .flightId(paymentDO.getFlightId())
                .flightNo(paymentDO.getFlightNo())
                .build();
        return noticeMapper.insert(build) > 0;
    }
}
